package me.zhengjie.modules.quartz.task;

import com.qiaofang.applet.common.util.RedisKeyUtil;
import com.qiaofang.applet.service.member.domain.EmployeeReport;
import com.qiaofang.applet.service.other.util.DateTypeUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class EmployeeReportRemoveTask {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisUtils redisUtils;

    public void run() {
        String taskKey = RedisKeyUtil.getTaskKey(this.getClass().getSimpleName());
        if (redisUtils.hasKey(taskKey)) {
            return;
        }
        redisUtils.set(taskKey, "", 1, TimeUnit.HOURS);
        log.info("EmployeeReportRemoveTask run start");
        Criteria criteria = Criteria.where("reportDate").lt(DateTypeUtil.getTodayInHistory(1));
        mongoTemplate.remove(new Query(criteria), EmployeeReport.class);
        log.info("EmployeeReportRemoveTask run end");
        redisUtils.del(taskKey);
    }
}
